package cn.stylefeng.guns.modular.cb.controller;

import cn.hutool.json.JSONObject;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.model.params.*;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerStaffResult;
import cn.stylefeng.guns.modular.cb.service.*;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 控制器
 *
 * @author wj
 * @Date 2021-01-07 22:44:39
 */
@Controller
@RequestMapping("/api/customer_staff")
public class CbCustomerStaffController extends BaseController {
    private String PREFIX = "/cb/cbCustomerStaff";

    @Autowired
    private CbCustomerStaffService cbCustomerStaffService;
    @Autowired
    private CbCustomerStudentsService cbCustomerStudentsService;

    @Autowired
    private UserService userService;

    @Autowired
    private CbMessagesService cbMessagesService;

    @Autowired
    private CbPositionService cbPositionService;

    @Autowired
    private CbFocusCollectionService cbFocusCollectionService;

    /**
     * 跳转到主页面
     *
     * @author wj
     * @Date 2021-03-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/cbCustomerStaff.html";
    }

    /**
     * 新增页面
     *
     * @author wj
     * @Date 2021-03-05
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/cbCustomerStaff_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wj
     * @Date 2021-03-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/cbCustomerStaff_edit.html";
    }

    @RequestMapping("/detail_p")
    public String detail() {
        return PREFIX + "/cbCustomerStaff_detail.html";
    }

    /**
     * 新增接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(CbCustomerStaffParam cbCustomerStaffParam) {
        LoginUser user = LoginContextHolder.getContext().getUser();

        this.cbCustomerStaffService.addNew(cbCustomerStaffParam);

//        CbCustomerStaffParam param = new CbCustomerStaffParam();
//        param.setCreateUser(user.getId());
//        CbCustomerStaffResult cbCustomerStaffResult = this.cbCustomerStaffService.getStaff(param);
//        cbCustomerStaffResult.setLoginUser(user);
//        return ResponseData.success(cbCustomerStaffResult);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(CbCustomerStaffParam cbCustomerStaffParam) {
        this.cbCustomerStaffService.update(cbCustomerStaffParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(CbCustomerStaffParam cbCustomerStaffParam) {
        this.cbCustomerStaffService.delete(cbCustomerStaffParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(CbCustomerStaffParam cbCustomerStaffParam) {
        CbCustomerStaffResult detail = this.cbCustomerStaffService.getStaff(cbCustomerStaffParam);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayuiPageInfo list(CbCustomerStaffParam cbCustomerStaffParam) {
        return this.cbCustomerStaffService.findPageBySpec(cbCustomerStaffParam);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/staff_list")
    @ResponseBody
    public LayuiPageInfo staffList(CbCustomerStaffParam cbCustomerStaffParam) {
        return this.cbCustomerStaffService.staffList(cbCustomerStaffParam);
    }

    /**
     * 学生的校友-学长
     * @param cbCustomerStaffParam
     * @return
     */
    @RequestMapping("/alumni_staff_list")
    @ResponseBody
    public LayuiPageInfo alumniByStaffList(CbCustomerStaffParam cbCustomerStaffParam) {
        LayuiPageInfo list = this.cbCustomerStaffService.alumniByStaffList(cbCustomerStaffParam);
        return list;
    }

    /**
     * 获取职员信息
     */
    @RequestMapping("/get_staff_user")
    @ResponseBody
    public ResponseData getStaffByUser(){
        Long userId = LoginContextHolder.getContext().getUserId();
        CbCustomerStaffParam param = new CbCustomerStaffParam();
        param.setCreateUser(userId);
        CbCustomerStaffResult cbCustomerStaffResult = this.cbCustomerStaffService.getStaffDetail(param);
        return ResponseData.success(cbCustomerStaffResult);
    }

    /**
     * 查看学长详情接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/staff_detail")
    @ResponseBody
    public ResponseData staffDetail(CbCustomerStaffParam cbCustomerStaffParam) {
        CbCustomerStaffResult detail = this.cbCustomerStaffService.staffDetail(cbCustomerStaffParam);
        return ResponseData.success(detail);
    }

    /**
     * 学生查询所有学长所在的公司
     * @param schoolNo
     * @return
     */
    @RequestMapping("/company_alumni")
    @ResponseBody
    public ResponseData companyByAlumni(String schoolNo) {
        List<JSONObject> companys = new ArrayList<>();
        List<CbCustomerStaffResult> list = this.cbCustomerStaffService.companyByAlumni(schoolNo);
        for (CbCustomerStaffResult result : list){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("companyId", result.getCompanyId());
            jsonObject.put("companyName", result.getCompanyName());
            companys.add(jsonObject);
        }
        return ResponseData.success(companys);
    }

    /**
     * 企业-删除员工
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/delete_staff_company")
    @ResponseBody
    public ResponseData deleteStaffCompany(CbCustomerStaffParam cbCustomerStaffParam) {
        this.cbCustomerStaffService.deleteStaffCompany(cbCustomerStaffParam.getId());
        return ResponseData.success();
    }
}


